package com.itheima.setlist;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SetListDemo2 {
    public static void main(String[] args) {
        method1();
        System.out.println("-----------------------------");
        method2();
    }

    public static void method1() {
        Set<Student1> s = new TreeSet<>((o1, o2) -> {
            int result = o1.getAge() - o2.getAge();
            result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            return result;
        });
        s.add(new Student1("定定",18));
        s.add(new Student1("宝宝定",17));
        s.add(new Student1("定宝",16));
        s.add(new Student1("定宝宝",18));

        Iterator<Student1> it = s.iterator();
        while(it.hasNext()){
            Student1 next = it.next();
            System.out.println(next);
        }
    }

    public static void method2() {
        Set<Student1> s = new TreeSet<>((o1, o2) -> {
            int result = o2.getAge() - o1.getAge();
            result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            return result;
        });
        s.add(new Student1("定定",18));
        s.add(new Student1("宝宝定",17));
        s.add(new Student1("定宝",16));
        s.add(new Student1("定宝宝",18));

        Iterator<Student1> it = s.iterator();
        while(it.hasNext()){
            Student1 next = it.next();
            System.out.println(next);
        }
    }
}

class Student1 {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student1() {
    }

    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}